package flatlandRPG;

import org.lwjgl.util.vector.Vector2f;
import java.io.File;
import java.io.IOException;
import org.lwjgl.input.Keyboard;

// -------------------------------------------------------------------------
/**
 * A game that simply draws a sphere to that rotates when you use the mouse.
 *
 * @author Evan Tatarka (evant)
 * @author trev
 * @version Apr 2, 2011
 */
public class MyGame
    extends Game
{
    private GameState     gameState;
    private Camera        camera;
    private Map           map;
    private GameText      textLabel;
    private HokiePassport passport;

    private boolean       showingTriggerText;
    private boolean       keyPressed;


    // ----------------------------------------------------------
    /**
     * Create a new MyGame object.
     *
     * @param width
     *            the width of the game
     * @param height
     *            the height of the game
     * @param drawPanel
     *            the panel to draw the game on in windowed mode
     * @param textLabel
     *            the GameText
     */
    public MyGame(int width, int height, DrawPanel drawPanel, GameText textLabel)
    {
        super(width, height, drawPanel);
        this.textLabel = textLabel;
    }


    @Override
    public void load()
    {
        gameState = new GameState();
        camera = new Camera();
        map = new Map();
        MapLoader loader = new MapLoader();
        try
        {
            map.add(loader.load(new File("src/map/player.map")));
            map.add(loader.load(new File("src/map/colors.map")));
            map.add(loader.load(new File("src/map/dorm.map")));
            map.add(loader.load(new File("src/map/pathToD2.map")));
            map.add(loader.load(new File("src/map/insideD2.map")));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        passport = new HokiePassport();

        loadActors();
        loadTriggers();

        textLabel.setVisible(false);
    }


    @Override
    public void update(long gameTime)
    {
        if (!TestableKeyboard.isKeyDown(Keyboard.KEY_E))
        {
            keyPressed = false;
        }

        if (TestableKeyboard.isKeyDown(Keyboard.KEY_R))
        {
            load();
        }

        if (TestableKeyboard.isKeyDown(Keyboard.KEY_Q))
        {
            stop();
        }

        if (!keyPressed && showingTriggerText
            && TestableKeyboard.isKeyDown(Keyboard.KEY_E))
        {
            textLabel.setVisible(false);
            showingTriggerText = false;
        }

        map.doLogic();
        camera.setLocation(map.getPlayer().getLocation());
    }


    @Override
    public void draw()
    {
        camera.draw();
        map.draw();
        if (passport != null)
        {
            passport.draw();
        }
    }


    private void loadActors()
    {
        Actor roomMate =
            new Actor(0.1f, 5, textLabel, "Hi There", "How are you doing");
        roomMate.setLocation(new Vector2f(.5f, .5f));

        Actor cashier1 = new Actor(0.1f, 5, textLabel, "Put text here");
        cashier1.setLocation(new Vector2f(53.7f, 52));

        Actor cashier2 = new Actor(0.1f, 5, textLabel, "Put text here");
        cashier2.setLocation(new Vector2f(53.7f, 52.8f));

        Actor d2exit = new Actor(0.1f, 5, textLabel, "Put text here");
        d2exit.setLocation(new Vector2f(52, 52));

        map.add(roomMate);
        map.add(cashier1);
        map.add(cashier2);
        map.add(d2exit);
    }


    private void loadTriggers()
    {
        /*
         * Teleport Triggers
         */

        // From dorm room --> bottom of dorm
        map.add(new TeleportTrigger(
            map.getPlayer(),
            new Vector2f(0.8f, 2.3f),
            new Vector2f(11.25f, 12.3f),
            .2f));

        // From bottom of D2 --> inside of D2
        map.add(new TeleportTrigger(map.getPlayer(), new Vector2f(
            8.285f,
            20.60f), new Vector2f(54.5f, 51.9f), 1));

        // Pick up Hokie passport
        map.add(new Trigger(
            map.getPlayer(),
            new Vector2f(1.15f, .85f),
            0.2f,
            new Action() {
                @Override
                public void activate()
                {
                    if (!gameState.eventOccured("Hokie Passport")
                        && Keyboard.isKeyDown(Keyboard.KEY_E))
                    {
                        gameState.addEvent("Hokie Passport");
                        textLabel
                            .setFormatedText("You have picked up your hokie passport");
                        textLabel.setVisible(true);
                        showingTriggerText = true;
                        keyPressed = true;
                        passport = null;
                    }
                }

            }));
    }
}
